﻿using PSI.Helper;
using PSI.Models.TMS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PSI.Services.TMS {
    public class RouteService : PSIBaseService {
        public List<Route> GetRouteListData() {
            var result = new List<Route>();

            var sql = "select id, f_route_name, f_km_h, f_km_l from tms_route order by f_route_name";

            Query(sql, reader => {
                result.Add(new Route {
                    Id = reader.GetString(0),
                    Name = reader.GetString(1),
                    KmH = reader.GetInt32(2),
                    KmL = reader.GetInt32(3)
                });
            });

            return result;
        }

        public Result Edit(Route route) {
            if (string.IsNullOrEmpty(route.Id)) {
                // 新增

                //检查线路是否已经存在
                var sql = "select count(*) from tms_route where f_route_name = @name";
                var p = CreateDbParameters();
                p.AddWithValue("name", route.Name);
                var cnt = QueryCount(sql, p);
                if (cnt > 0) {
                    return new Result { Success = false, Messgae = "线路已经存在" };
                }

                route.Id = IdGeneratorService.NewId();

                sql = @"insert into tms_route(id, f_route_name, f_km_h, f_km_l)
                        values(@id, @name, @kmH, @kmL)";
                p = CreateDbParameters();
                p.AddWithValue("id", route.Id);
                p.AddWithValue("name", route.Name);
                p.AddWithValue("kmH", route.KmH);
                p.AddWithValue("kmL", route.KmL);

                Execute(sql, p);

            } else {
                // 编辑

                // 检查线路是否已经存在
                var sql = @"select count(*) from tms_route 
                            where f_route_name = @name and id <> @id";
                var p = CreateDbParameters();
                p.AddWithValue("name", route.Name);
                p.AddWithValue("id", route.Id);
                var cnt = QueryCount(sql, p);
                if (cnt > 0) {
                    return new Result { Success = false, Messgae = "线路已经存在" };
                }

                sql = @"update tms_route
                        set f_route_name = @name, f_km_h = @kmH, f_km_l = @kmL
                        where id = @id";
                p = CreateDbParameters();
                p.AddWithValue("id", route.Id);
                p.AddWithValue("name", route.Name);
                p.AddWithValue("kmH", route.KmH);
                p.AddWithValue("kmL", route.KmL);

                Execute(sql, p);
            }

            return new Result { Success = true, Id = route.Id };
        }

        public Result Delete(string id) {
            var sql = "delete from tms_route where id = @id";
            var p = CreateDbParameters();
            p.AddWithValue("id", id);

            Execute(sql, p);

            return new Result { Success = true };
        }
    }
}